package com.ymt.bpm.service.mobile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.appboot.env.Caches;
import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.dao.Mapper;
import com.ymt.bpm.model.PI18n;
import com.ymt.bpm.service.platform.cache.CacheService;
import com.ymt.bpm.util.Const;
import com.ymt.fmd.model.PFormVersion;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Johnny on 2017/11/16.
 */
@Service
public class MFmdService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MDao dao;

    @Autowired
    private CacheService cacheService;

    /**
     * 获取表单信息，表单版本更新后需要清理缓存
     * @param tenantId
     * @param formid
     * @param versionid
     * @return
     * @see FmdService.saveForm for cache evict
     */
    //@Cacheable(value= Caches.FORM, key="'getFormData_'+#tenantId+'_'+#formid+'_'+#versionid")
    public JSONObject getFormData(String tenantId, String formid, String versionid) {
        String cacheKey = cacheService.asKey(Caches.FormPrefix.IDVER_FORMDATA.name(), tenantId, formid, versionid);
        JSONObject rtn = cacheService.get(Caches.FORM, cacheKey);
        if (rtn==null) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
                paramMap.put(Const.TENANT_ID, tenantId);
            }
            paramMap.put("formid", formid);
            paramMap.put("versionid", versionid);
            PFormVersion version = dao.selectOne(Mapper.PFormVersionMapper, "selectFormData", paramMap);
            rtn = JSONObject.parseObject(version.getFormdata());
            if (rtn!=null) {
                cacheService.put(Caches.BPD, cacheKey, rtn);
            }
        }
        return rtn;
    }

    //TODO @Cacheable
    public JSONObject getDictTypes(String tenantId, String lang, String typesStr) {
        JSONObject rtn = new JSONObject();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(Const.LANG, lang);
        paramMap.put("category", "ENUMERATION");
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            paramMap.put(Const.TENANT_ID, tenantId);
        }
        String[] typeAr = typesStr.split(",");
        StringBuilder sb = new StringBuilder();
        if (typeAr!=null && typeAr.length>0) {
            for (int i=0; i<typeAr.length; i++) {
                if (i>0) {
                    sb.append(",");
                }
                sb.append("'").append(typeAr[i]).append("'");
            }
            paramMap.put("types", sb.toString());
            List<PI18n> list = dao.selectList(Mapper.PI18nMapper, "selectByTypes", paramMap);
            if (list!=null && list.size()>0) {
                String lastType = null;
                JSONObject dict = null;
                for (PI18n pi18n : list) {
                    if (!pi18n.getType().equalsIgnoreCase(lastType)) {
                        if (dict!=null && !dict.isEmpty()) {
                            rtn.put(lastType, dict);
                        }
                        dict = new JSONObject();
                    }
                    dict.put(pi18n.getRelationkey(), pi18n.getValue());
                    lastType = pi18n.getType();
                }
                if (dict!=null && !dict.isEmpty()) {
                    rtn.put(lastType, dict);
                }
            }
        }

        return rtn;
    }
}
